package ie.dcu.spca.view;

import java.util.Map;

import ie.dcu.spca.R;
import ie.dcu.spca.data.BasicDataManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public abstract class CRUDDialog extends Activity {

    protected Bundle bundle;
    protected ImageButton cancelButton;
    protected int id;
    protected boolean isAdd;
    protected ImageButton okButton;
    protected ImageButton removeButton;

    public CRUDDialog() {
        super();
    }

    private void addOkButtonClick(View v) {
        if (!initializeValuesFromWidget())
            return;
        addOkButtonClickOpt(v);
        this.finish();
    }

    protected abstract void addOkButtonClickOpt(View v);

    protected void afterOnCreateInvoke(Bundle savedInstanceState) {
        setValueFromIntentExtras();
        bindWidget();

        if (isAdd)
            createBeanOpt();
        else
            updateBeanOpt();
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelButtonClick(v);
            }
        });
    }

    protected void beforeOnCreateInvoke(Bundle savedInstanceState) {
    }

    private void bindWidget() {
        atBeginOfBindWidgetOnCreate();
        removeButton = (ImageButton) findViewById(R.id.deleteDataButton);
        okButton = (ImageButton) findViewById(R.id.okOptButton);
        cancelButton = (ImageButton) findViewById(R.id.cancelOptButton);
        bindWidgetOnCreate();
    }

    protected void atBeginOfBindWidgetOnCreate() {
    }

    protected abstract void bindWidgetOnCreate();

    private void cancelButtonClick(View v) {
        cancelButtonClickOpt(v);
        this.finish();
    }

    protected void cancelButtonClickOpt(View v) {
    }

    private void createBeanOpt() {
        removeButton.setClickable(false);
        removeButton.setVisibility(View.INVISIBLE);
        okButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                addOkButtonClick(v);
            }
        });
    }

    private void deleteBeanOkButtonClick(DialogInterface dialog) {
        deleteBeanOkButtonClickOpt(dialog);
        this.finish();
    }

    protected abstract void deleteBeanOkButtonClickOpt(DialogInterface dialog);

    protected int getContentView() {
        return R.layout.usercruddia;
    }

    protected abstract String getDeleteDataConfirmTip();

    protected abstract boolean initializeValuesFromWidget();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        beforeOnCreateInvoke(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        afterOnCreateInvoke(savedInstanceState);
    }

    private void setValueFromIntentExtras() {
        bundle = this.getIntent().getExtras();
        isAdd = bundle.getBoolean(DataList.IS_ADD);
        setValueFromIntentExtrasOnCreate();
    }

    protected abstract void setValueFromIntentExtrasOnCreate();

    private void updateBeanOpt() {
        id = bundle.getInt(BasicDataManager.ID);

        updateBeanWidgetInitialize();

        removeButton.setClickable(true);
        okButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                updateOkButtonClick(v);
            }
        });
        removeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        CRUDDialog.this);
                builder.setTitle(getDeleteDataConfirmTip()).setCancelable(true)
                        .setPositiveButton(R.string.yesButton,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        deleteBeanOkButtonClick(dialog);
                                    }
                                }).setNegativeButton(R.string.noButton,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });

    }

    public static void setSpinnerSelector(Spinner spinner, int valueId) {
        int i = 0;
        SpinnerAdapter adapter = spinner.getAdapter();
        while (i < adapter.getCount()) {
            Map<String, Object> item = (Map<String, Object>) adapter.getItem(i);
            if (valueId == (Integer) item.get(BasicDataManager.ID))
                break;
            i++;
        }
        spinner.setSelection(i, true);
    }

    protected abstract void updateBeanWidgetInitialize();

    private void updateOkButtonClick(View v) {
        if (!initializeValuesFromWidget())
            return;
        updateOkButtonClickOpt(v);
        this.finish();
    }

    protected abstract void updateOkButtonClickOpt(View v);

}
